package com.abc.cloud.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.abc.cloud.dto.menu.MenuDto;
import com.abc.cloud.dto.menu.MenuTree;
import com.abc.cloud.vo.menu.MenuSave;
import com.abc.cloud.vo.menu.MenuUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.abc.cloud.entity.MenuEntity;
import com.abc.cloud.service.MenuService;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Result;



/**
 * 菜单表
 *
 * @author LiJunXi
 * @email 2770063826@qq.com
 * @date 2023-10-27 13:09:54
 */
@Api(tags = "菜单接口")
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtil page = menuService.queryPage(params);

        return Result.success(page, "page");
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
		MenuEntity menu = menuService.getById(id);

        return Result.success(menu, "menu");
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody MenuEntity menu){
		menuService.save(menu);

        return Result.success(null, "操作成功");
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody MenuEntity menu){
		menuService.updateById(menu);

        return Result.success(null, "操作成功");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
		menuService.removeByIds(Arrays.asList(ids));

        return Result.success(null, "操作成功");
    }

    @ApiOperation("获取所有菜单列表")
    @GetMapping("/getMenuList")
    public Result<List<MenuTree>> getMenuList(){
        List<MenuTree> adminMenuTrees = menuService.getMenuList();
        return Result.success(adminMenuTrees, "获取菜单列表成功");
    }

    @ApiOperation("更新菜单")
    @PutMapping("/updateMenu")
    public Result<MenuEntity> updateMenu(@RequestBody MenuUpdate adminMenuUpdate){
        MenuEntity tbMenu = menuService.updateMenu(adminMenuUpdate);
        if (tbMenu == null){
            return Result.fail(null, "修改后台出错");
        }
        return Result.success(tbMenu, "修改菜单成功");
    }

    @ApiOperation("保存菜单")
    @PostMapping("/saveMenu")
    public Result<MenuEntity> saveMenu(@RequestBody MenuSave adminMenuSave){
        MenuEntity tbMenu = menuService.saveMenu(adminMenuSave);
        if (tbMenu == null){
            return Result.fail(null, "新增菜单出错");
        }
        return Result.success(tbMenu, "新增菜单成功");
    }

    @ApiOperation("获取所有父菜单")
    @GetMapping("/getParentMenu")
    public Result<List<MenuEntity>> getParentMenu(){
        List<MenuEntity> tbMenuList = menuService.getParentMenu();
        return Result.success(tbMenuList, "查询所有父菜单成功");
    }

    @ApiOperation("获取用户菜单")
    @GetMapping("/admin/getUserMenu")
    public Result<List<MenuDto>> getUserMenu(@RequestParam("username") String username){
        List<MenuDto> tbMenuList = menuService.getUserMenu(username);
        return Result.success(tbMenuList, "获取用户菜单成功");
    }


}
